{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2024-09-02 18:46:08,870 - modelscope - INFO - PyTorch version 2.3.0 Found.\n",
      "2024-09-02 18:46:08,870 - modelscope - INFO - Loading ast index from /home/jie/.cache/modelscope/ast_indexer\n",
      "2024-09-02 18:46:08,884 - modelscope - INFO - Loading done! Current index file version is 1.14.0, with md5 9f53011443b7fabdfb807148bea6e1f2 and a total number of 976 components indexed\n"
     ]
    }
   ],
   "source": [
    "from settings import LABEL_NAMES\n",
    "from utils import load_obj\n",
    "\n",
    "from datasets import load_dataset\n",
    "\n",
    "import os\n",
    "\n",
    "os.environ[\"HTTP_PROXY\"] = \"http://127.0.0.1:7890\"\n",
    "os.environ[\"HTTPS_PROXY\"] = \"http://127.0.0.1:7890\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "LLM_OUTPUT_OBJ = \"objs/5w_cls_240902.pkl\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "dataset = load_dataset(\n",
    "    \"csv\",\n",
    "    # data_files='data/all_test_best_llm.csv',\n",
    "    data_files=\"../hydrogen/data_clean/全国5万家氢能企业名单_utf8.csv\",\n",
    "    split=\"train\",\n",
    ")\n",
    "all_res = load_obj(LLM_OUTPUT_OBJ)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "labels = []\n",
    "reasons = []\n",
    "\n",
    "err = 0\n",
    "for output in all_res:\n",
    "    output_text = output.outputs[0].text\n",
    "    pred_label = output_text.split(\"label:\")[-1].strip()\n",
    "    pred_label = pred_label.split(\"\\n\")[0].strip()\n",
    "\n",
    "    reasons.append(\n",
    "        output_text.replace(\"label:\", \"\")\n",
    "        .replace(pred_label, \"\")\n",
    "        .replace(\"  \", \"\")\n",
    "        .strip()\n",
    "    )\n",
    "\n",
    "    cnt = 0\n",
    "    tmp_res = []\n",
    "    for label in LABEL_NAMES:\n",
    "        if label in pred_label:\n",
    "            tmp_res.append(label)\n",
    "            cnt += 1\n",
    "\n",
    "    if cnt > 1:\n",
    "        err += 1\n",
    "        # print(cnt, output.outputs[0].text)\n",
    "        # print(cnt, pred_label)\n",
    "        # print('=' * 50)\n",
    "    labels.append(\"、\".join(tmp_res))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "new_dataset = dataset.add_column(\"氢能产业链节点\", labels)\n",
    "new_dataset = new_dataset.add_column(\"上链原因\", reasons)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "core_dataset = new_dataset.filter(\n",
    "    lambda x: x[\"氢能产业链节点\"] != \"其他\" and x[\"氢能产业链节点\"].strip()\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Dataset({\n",
       "    features: ['企业名称', '英文名称', '统一社会信用代码', '企业类型', '经营状态', '成立日期', '核准日期', '法定代表人', '注册资本', '实缴资本', '参保人数', '公司规模', '经营范围', '注册地址', '营业期限', '纳税人识别号', '工商注册号', '组织机构代码', '纳税人资质', '曾用名', '所属省份', '所属城市', '所属区县', '网站链接', '所属行业', '一级行业分类', '二级行业分类', '三级行业分类', '登记机关', '经度', '纬度', '网址', '氢能产业链节点', '上链原因'],\n",
       "    num_rows: 18442\n",
       "})"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "core_dataset"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "筛选出 京津冀 的企业"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "f9c1346dea2b43af8b6168426b4683ab",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Filter:   0%|          | 0/51443 [00:00<?, ? examples/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "area_dataset = new_dataset.filter(\n",
    "    lambda x: x[\"所属省份\"] in [\"北京市\", \"河北省\", \"天津市\"]\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Dataset({\n",
       "    features: ['企业名称', '英文名称', '统一社会信用代码', '企业类型', '经营状态', '成立日期', '核准日期', '法定代表人', '注册资本', '实缴资本', '参保人数', '公司规模', '经营范围', '注册地址', '营业期限', '纳税人识别号', '工商注册号', '组织机构代码', '纳税人资质', '曾用名', '所属省份', '所属城市', '所属区县', '网站链接', '所属行业', '一级行业分类', '二级行业分类', '三级行业分类', '登记机关', '经度', '纬度', '网址', '氢能产业链节点', '上链原因'],\n",
       "    num_rows: 5071\n",
       "})"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "area_dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "area_dataset[\"\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "针对每个大类别细分出小类别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "llm",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
